// initial state
const state = {
  isOnline: false,
  token: null,
  info: {},
  point: null,
  inviterId: 0,
  inviterTime: 0,
  isTokenInvalid: false,
  payOpenId: null
}

// mutations
const mutations = {
  memberInviterId(state, payload) {
    // console.log(payload.inviterId, '存储的ID')
    state.inviterId = payload.inviterId
    state.inviterTime = new Date().getTime() + 3600000 // 一小时过期
  },
  setPayOpenId(state, payload) {
    state.payOpenId = payload
  },
  memberPoint(state, payload) {
    state.point = payload.point
  },
  memberLogin(state, payload) {
    state.info = payload.info
    state.info.invalid_time = parseInt(new Date().getTime() / 1000) + 3600 // 一小时更新
    state.isOnline = true
    state.token = payload.token
    state.isTokenInvalid = false
  },
  memberLogout(state) {
    state.info = null
    state.isOnline = false
    state.token = null
    state.payOpenId = null
  },
  memberUpdate(state, payload) {
    if (state.info) {
      state.info = payload.info
      state.info.invalid_time = parseInt(new Date().getTime() / 1000) + 3600 // 一小时更新
    }
  },
  memberEdit(state, payload) {
    if (state.info) {
      let i
      for (i in payload) {
        state.info[i] = payload[i]
      }
    }
  }
}

export default {
  state,
  mutations
}
